package com.newasia.server.common.interceptor.page;

import jodd.util.StringUtil;
import lombok.Data;

import java.util.ArrayList;

/**
 * @author LiChong
 * @description 分页参数
 * @date 2020/5/28
 */

@Data
public class PageParam <E> extends ArrayList<E> {
    //开始页
    private int pageNum;
    //每页数量
    private int pageSize;
    //开始行
    private int startRow;
    //结束行
    private int endRow;
    //总数量
    private long total;
    //总页数
    private int pages;
    //排序
    private String orderBy;
    //是否为第一页
    private boolean isFirstPage = false;
    //是否为最后一页
    private boolean isLastPage = false;
    //是否有前一页
    private boolean hasPreviousPage = false;
    //是否有下一页
    private boolean hasNextPage = false;

    public PageParam(int pageNum,int pageSize,String orderBy,boolean isAscend){
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.startRow = pageNum > 1 ? (pageNum - 1) * pageSize : 0;
        this.isFirstPage = (pageNum > 1);
        this.hasPreviousPage = !isFirstPage;
        this.endRow = pageNum * pageSize;
        StringBuilder builder = new StringBuilder("order by ");
        this.orderBy = builder.append((StringUtil.isEmpty(orderBy) ? "1" : orderBy))
                .append((isAscend ? " asc" : " desc")).toString();
    }

    public void setPages(int totalPage){
        this.pages = totalPage;
        this.isLastPage = (totalPage==pageNum);
        this.hasNextPage = !isLastPage;
    }
}
